package view;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.ImageIcon;
import javax.swing.JPanel;

import model.ModelReader;

import util.Coord;
import controller.Controller;

public class CraftPanel extends JPanel implements MouseListener {
	private static final long serialVersionUID = 1L;
	private ModelReader mr;
	private Stage parent;
	private Controller controller;
	private int TILE_HEIGHT = 69;
	private int TILE_WIDTH = 80;

	public CraftPanel(Stage parent, Controller controller) {
		this.mr = parent.getModelReader();
		this.parent = parent;
		this.controller = controller;
		this.addMouseListener(this);
		init();
	}

	protected void init() {
		setBackground(Color.blue);
		repaint();
	}

	@Override
	public void paintComponent(Graphics g){
		super.paintComponent(g);
		String path = null;
		TILE_HEIGHT = (getHeight() - 10)/mr.getTableHeight();
		TILE_WIDTH = TILE_HEIGHT*100/86;
		for(int x = 0; x < mr.getTableWidth(); x++) {
			for(int y = 0; y < mr.getTableHeight(); y++) {
				int id = mr.getTableID(x,y);
				switch(id) {
				case 1:
					path = "/resource/CraftTable/Stick.png";
					break;
				case 2:
					path = "/resource/CraftTable/Shale.png";
					break;
				case 3:
					path = "/resource/CraftTable/Wood.png";
					break;
				case 4:
					path = "/resource/CraftTable/Iron.png";
					break;
				case 6:
					path = "/resource/CraftTable/Stone.png";
					break;
				default:
					path = "/resource/CraftTable/Hexagon.png";
					break;
				}
				ImageIcon image = new ImageIcon(getClass().getResource(path));
				g.drawImage(image.getImage(), (int)gridToView(x, y).getX(), (int)gridToView(x, y).getY(), TILE_WIDTH, TILE_HEIGHT, null);
			}
		}
		int id = mr.getOutputID();
		switch(id) {
		case 1:
			path = "/resource/CraftTable/Stick.png";
			break;
		case 2:
			path = "/resource/CraftTable/Shale.png";
			break;
		case 3:
			path = "/resource/CraftTable/Wood.png";
			break;
		case 4:
			path = "/resource/CraftTable/Iron.png";
			break;
		case 6:
			path = "/resource/CraftTable/Stone.png";
			break;
		case 50:
			path = "/resource/CraftTable/shale_axe.png";
			break;
		case 100:
			path = "/resource/CraftTable/tipi.png";
			break;
		default:
			path = "/resource/CraftTable/Hexagon.png";
			break;
		}
		ImageIcon image = new ImageIcon(getClass().getResource(path));
		g.drawImage(image.getImage(), getWidth() - 100 - 5, getHeight() - 86 - 5, null);
	}

	protected void update() {
		repaint();
	}
	
	private Coord gridToView(int x, int y){
		return new Coord(x*TILE_WIDTH*3/4 + y*TILE_WIDTH*3/4 + getWidth()/2 - TILE_WIDTH/2 - (mr.getTableWidth() - 1)*TILE_WIDTH*3/4, y*TILE_HEIGHT/2 + (mr.getTableWidth() - 1)*TILE_HEIGHT/2 - x*TILE_HEIGHT/2 + 5);
	}

	private Coord viewToGrid(int vx, int vy) {
		Coord closest = new Coord(-1, -1);
		int distance = TILE_WIDTH/2;
		for(int y = 0; y < mr.getTableHeight(); y++){
			for(int x = 0; x < mr.getTableWidth(); x++){
				Coord view = new Coord(gridToView(x, y).getX() + TILE_WIDTH/2, gridToView(x, y).getY() + TILE_HEIGHT/2);
				int temp = (int)Math.sqrt((view.getX() - vx)*(view.getX() - vx) + (view.getY() - vy)*(view.getY() - vy));
				if(temp < distance){
					closest = new Coord(x, y);
				}
			}
		}
		if(!closest.equals(new Coord(-1, -1))){
			return closest;
		}
		distance = (int)Math.sqrt((getWidth() - 55 - vx)*(getWidth() - 55 - vx) + (getHeight() - 48 -vy)*(getHeight() - 48 - vy));
		if(distance < 50)
			return new Coord(-1, -1);
		return null;
	}

	@Override
	public void mouseClicked(MouseEvent e) {
		Coord gc = new Coord(viewToGrid(e.getX(), e.getY()));
		switch(e.getButton()){
		case MouseEvent.BUTTON1:
			controller.leftClickCrafting(gc.getX(), gc.getY());          
			break;
		case MouseEvent.BUTTON3:
			controller.rightClickCrafting(gc.getX(), gc.getY());
			break;
		}
	}

	@Override
	public void mouseEntered(MouseEvent e) {
		// TODO Auto-generated method stub

	}

	@Override
	public void mouseExited(MouseEvent e) {
		// TODO Auto-generated method stub

	}

	@Override
	public void mousePressed(MouseEvent e) {
		// TODO Auto-generated method stub

	}

	@Override
	public void mouseReleased(MouseEvent e) {
		// TODO Auto-generated method stub

	}

}
